package com.ruoyi.system.wnjk.service.impl;

import com.ruoyi.system.wnjk.domain.po.WnjkPatient;
import com.ruoyi.system.wnjk.domain.vo.RegistrationOrderVO;
import com.ruoyi.system.wnjk.domain.vo.doctorVO;
import com.ruoyi.system.wnjk.domain.vo.patientVO;
import com.ruoyi.system.wnjk.mapper.RegistrationOrderMapper;
import com.ruoyi.system.wnjk.service.RegistrationOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RegistrationOrderServiceImpl implements RegistrationOrderService {

    @Autowired
    private RegistrationOrderMapper registrationOrderMapper;

    @Override
    public List<RegistrationOrderVO> getAllRegistrationOrders() {
        return registrationOrderMapper.getRegistrationOrderVOList();
    }

    @Override
    public List<doctorVO> getDoctorsByHospitalId(Long Id) {
        return registrationOrderMapper.getDoctorsByHospitalId(Id);
    }

    @Override
    public doctorVO getDoctorById(Long doctorId) {
        return registrationOrderMapper.getDoctorById(doctorId);
    }

    @Override
    public List<patientVO> getPatientsByUserId(Long userId) {
        List<patientVO> patients = registrationOrderMapper.getPatientsByUserId(userId);

        // 设置固定值
        for (patientVO patient : patients) {
            patient.setPhone("133 ****9999");
            patient.setLocal("北京");
        }

        return patients;
    }

    @Override
    public boolean deletePatient(Long patientId) {
        // 先删除用户与患者的关联
        registrationOrderMapper.deleteUserPatientRelationByPatientId(patientId);

        // 再删除患者信息
        int result = registrationOrderMapper.deletePatientById(patientId);

        // 返回删除成功状态
        return result > 0;
    }

    @Override
    public boolean addPatient(Long userId, WnjkPatient patient) {
        // 插入患者信息
        int insertPatientResult = registrationOrderMapper.insertPatient(patient);

        if (insertPatientResult > 0 && patient.getId() != null) {
            // 插入关联关系
            int relationResult = registrationOrderMapper.insertUserPatientRelation(userId, patient.getId());
            return relationResult > 0;
        }

        return false;
    }
}
